<template>
  <div>
    <div class="col-xs-12">
      <div class="input-group input-group-lg">
        <span id="sizing-addon1" class="input-group-addon">Choose a file</span>

        <span class="control-fileupload">
          <span class="label">{{ fileName }}</span>
          <input
            id="files"
            ref="files"
            class="form-control"
            type="file"
            multiple
            @change="handleFilesUploads()"
          />
        </span>

        <span class="input-group-btn">
          <button class="btn btn-cta" @click="submitFiles()">Install</button>
        </span>
      </div>
      <!-- /input-group -->
    </div>
  </div>
</template>
<script>
import { defineComponent } from "vue";
import { mapActions } from "vuex";
import axios from "axios";
import { client } from "../../../../library/modules/rundeckClient";
export default defineComponent({
  name: "UploadPluginForm",
  computed: {
    fileName() {
      if (this.files && this.files[0] && this.files[0].name) {
        return this.files[0].name;
      } else {
        return "";
      }
    },
  },
  methods: {
    ...mapActions("overlay", ["openOverlay", "closeOverlay"]),
    submitFiles() {
      // Initialize the form data and iteate over any
      // file sent over appending the files to the form data.
      const formData = new FormData();
      for (let i = 0; i < this.files.length; i++) {
        const file = this.files[i];

        formData.append("pluginFile", file);
      }
      this.openOverlay({
        loadingMessage: "Installing",
        loadingSpinner: true,
      });
      //use axios instead of RundeckClient, to allow multipart form with file upload
      axios({
        method: "post",
        headers: {
          "x-rundeck-ajax": true,
          "Content-Type": "multipart/form-data",
          "X-RUNDECK-TOKEN-KEY": client.token,
          "X-RUNDECK-TOKEN-URI": client.uri,
        },
        data: formData,
        url: `${window._rundeck.rdBase}plugin/uploadPlugin`,
        withCredentials: true,
      })
        .then((response) => {
          this.closeOverlay();
          client.token =
            response.headers["x-rundeck-token-key"] || client.token;
          client.uri = response.headers["x-rundeck-token-uri"] || client.uri;
          if (response.data.err) {
            this.$alert({
              title: "Error Uploading",
              content: response.data.err,
            });
          } else {
            this.$alert({
              title: "Plugin Installed",
              content: response.data.msg,
            });
          }
        })
        .catch((result) => {
          this.closeOverlay();
          let message = result.message;
          if (
            result.response &&
            result.response.data &&
            result.response.data.message
          ) {
            message = result.response.data.message;
          }
          this.$alert({
            title: "Error Uploading",
            content: message,
          });
        });
    },
    handleFilesUploads() {
      this.files = this.$refs.files.files;
    },
  },
  data() {
    return {
      files: "",
    };
  },
  created() {},
});
</script>
<style lang="scss" scoped>
/* input [type = file]
----------------------------------------------- */

input[type="file"] {
  display: block !important;
  right: 1px;
  top: 1px;
  height: 55px;
  opacity: 0;
  width: 100%;
  background: none;
  position: absolute;
  overflow: hidden;
  z-index: 2;
}

.control-fileupload {
  display: block;
  background-color: var(--input-bg-color);
  border: 1px solid var(--input-outline-color);
  // border-radius: 4px;
  width: 100%;
  height: 55px;
  line-height: 36px;
  padding: 0px 10px 2px 10px;
  overflow: hidden;
  position: relative;
  border-right: 0;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;

  &:before,
  input,
  .label {
    cursor: pointer !important;
  }
  /* File upload button */
  &:before {
    padding: 4px 12px;
    margin-bottom: 0;
    margin-right: 10px;
    margin-top: 9px;
    font-size: 14px;
    line-height: 20px;
    color: #333333;
    text-align: center;
    text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
    vertical-align: middle;
    cursor: pointer;
    background-color: #f5f5f5;
    background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
    background-repeat: repeat-x;
    border: 1px solid #cccccc;
    border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25);
    border-bottom-color: #b3b3b3;
    border-radius: 4px;
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.2),
      0 1px 2px rgba(0, 0, 0, 0.05);
    transition: color 0.2s ease;
    content: "Browse";
    display: block;
    position: absolute;
    z-index: 1;
    top: 2px;
    right: 2px;
    line-height: 20px;
    text-align: center;
  }
  &:hover,
  &:focus {
    &:before {
      color: #333333;
      background-color: #e6e6e6;
      text-decoration: none;
      background-position: 0 -15px;
      transition: background-position 0.2s ease-out;
    }
  }

  .label {
    line-height: 41px;
    color: #999999;
    font-size: 20px;
    font-weight: normal;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    position: relative;
    z-index: 1;
    margin-right: 90px;
    margin-bottom: 0px;
    margin-top: 4px;
    cursor: text;
  }
}
</style>
